﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using GameFramework.Controller;
using Microsoft.Xna.Framework;

namespace MyRaceGame.GameLogic.GameStates
{
    public class HtPMenuState : RaceGameState
    {
        public HtPMenuState(RaceGame raceGame)
            : base(raceGame)
        {
            InputController.MouseMoved += new InputControl.MouseMoveEventHandler(InputController_MouseMoved);
            InputController.Clicked += new InputControl.ClickEventHandler(InputController_Clicked);
            
            RaceGameState.LoadTextureFrom(raceGame, ref rightLeftTex01, "MenuImages", "right_left01");
            RaceGameState.LoadTextureFrom(raceGame, ref rightLeftTex02, "MenuImages", "right_left02");
            RaceGameState.LoadTextureFrom(raceGame, ref backTex, "MenuImages", "backm");
            RaceGameState.LoadTextureFrom(raceGame, ref backTexS, "MenuImages", "backm_selected");

            backMenuItem = new MenuItem("Back", backTex, backTexS, new Vector2(384, 300));
           
            raceGame.IsMouseVisible = true;

            if (!raceGame.Graphics.IsFullScreen)
                raceGame.ToggleFullScreen();
        }

        void InputController_Clicked(bool right, Vector2 position)
        {
            if (right)
                return;

            if (!backMenuItem.Bounds.Contains((int)position.X, (int)position.Y))
                return;


            InputController.Clicked -= InputController_Clicked;
            InputController.MouseMoved -= InputController_MouseMoved;
            GameState = new MainMenuState(raceGame);
        }

        protected override void UpdateAdditional(GameTime gameTime)
        {
            
        }

        void InputController_MouseMoved(Vector2 position)
        {
            backMenuItem.Selected = backMenuItem.Bounds.Contains((int)position.X, (int)position.Y);
        }

        protected override void DrawingAdditional(GameTime gameTime)
        {
            raceGame.SpriteBatch.Begin();

            raceGame.SpriteBatch.Draw(MainMenuState.BackTex, Vector2.Zero, Color.White);

            elapsedTime += gameTime.ElapsedGameTime;

            ///TODO: Design a complete animation class
            int millis = elapsedTime.Milliseconds % 1000;
            
            if ((millis >= 0 && millis < 250) || (millis >= 500 && millis < 750))
                raceGame.SpriteBatch.Draw(rightLeftTex01, new Vector2(20, 20), Color.White);
            else
                raceGame.SpriteBatch.Draw(rightLeftTex02, new Vector2(20, 20), Color.White);
            

            if (backMenuItem.Selected)
                raceGame.SpriteBatch.Draw(backMenuItem.SelectedTexture, backMenuItem.Position, Color.White);
            else
                raceGame.SpriteBatch.Draw(backMenuItem.Texture, backMenuItem.Position, Color.White);

            raceGame.SpriteBatch.End();
        }

        protected override void DrawText(Microsoft.Xna.Framework.GameTime gameTime)
        {
            TextDrawController.Begin(Color.YellowGreen, TextAlignment.Left);

            TextDrawController.WriteLine();
            TextDrawController.WriteLine();
            TextDrawController.WriteLine();
            TextDrawController.WriteLine();
            TextDrawController.WriteLine();
            TextDrawController.WriteLine();
            TextDrawController.WriteLine("     Press right and left rapidly to gain acceleration");

            TextDrawController.End();

            TextDrawController.Begin(Color.Tomato, TextAlignment.Center);

            TextDrawController.WriteLine();
            TextDrawController.WriteLine("Press D for debug mode");
            TextDrawController.WriteLine("Press C for Chase Camera Mode (default camera mode)");
            TextDrawController.WriteLine("Press F for Free Camera Mode");
            TextDrawController.WriteLine("Press Esc for exiting the game");

            TextDrawController.End();

            TextDrawController.Begin(Color.YellowGreen, TextAlignment.Right);

            TextDrawController.WriteLine();
            TextDrawController.WriteLine("Chase Camera");
            TextDrawController.WriteLine("------------");
            TextDrawController.WriteLine("Press Q for spectating other racers view");
            TextDrawController.WriteLine();
            TextDrawController.WriteLine("Free Camera");
            TextDrawController.WriteLine("-----------");
            TextDrawController.WriteLine("Use WASD keys to move the camera");
            TextDrawController.WriteLine("Use mouse to rotate the camera");

            TextDrawController.End();
        }

        

        static Texture2D rightLeftTex01, rightLeftTex02, backTex, backTexS;
        TimeSpan elapsedTime;

        MenuItem backMenuItem;
    }
}
